package com.wing.product.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.contract.model.request.WarehouseContractForm;
import com.wing.product.model.entity.WarehouseOrder;
import com.wing.product.model.request.WarehouseSettledForm;
import com.wing.product.model.request.WarehouseShipmentOrderForm;
import com.wing.product.model.response.WarehouseOrderVO;
import com.wing.product.model.response.WarehouseSettledVO;
import com.wing.product.service.WarehouseSettledService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "托管入驻管理", tags = "#/warehouse/product/settled")
@RestController("WarehouseSettledController")
@RequestMapping("/warehouse/settled")
@RequiredArgsConstructor
public class WarehouseSettledController {


    @Autowired
    private WarehouseSettledService warehouseSettledService;

    @ApiOperation(value = "查看托管入驻申请", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseOrderVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order
            ,String settledOrder

    ){
        Page<WarehouseOrder> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("settledOrder",settledOrder);
        //判断客户端，如果是会员端，则需要添加对应的部门信息作为查询条件
        IPage<WarehouseOrderVO> list = warehouseSettledService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加入驻申请", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@Validated(WarehouseSettledForm.class)  @RequestBody WarehouseSettledForm WarehouseSettledForm)  {
        try{

            Boolean b =  warehouseSettledService.save(WarehouseSettledForm);
            if(b){
                return JsonResult.success("添加成功");
            } else {
                return JsonResult.fail("添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "撤销", notes = "撤销")
    @GetMapping("/revoke")
    public JsonResult revoke(@Param("id")Long id ,@Param("status")Integer status )  {
        try{
            return  warehouseSettledService.updateStatus(id,status);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "审核", notes = "审核")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/examine")
    public JsonResult examine(@Validated(WarehouseContractForm.Add.class)  @RequestBody WarehouseContractForm warehouseContractForm)  {
        try{

            Boolean b =  warehouseSettledService.examine(warehouseContractForm);
            if(b){
                return JsonResult.success("审核成功");
            } else {
                return JsonResult.fail("审核失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "详情", notes = "详情")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/info")
    public JsonResult info(Long id ) {
        return JsonResult.success(warehouseSettledService.info(id));
    }


    @ApiOperation(value = "根据仓库查联系人", notes = "搜索")
    @GetMapping("/findAllContactByFactoryId")
    public JsonResult<WarehouseSettledVO> findAllContactByFactoryId(@Param("factoryId")String factoryId,@Param("warehouseCustomerId")Long warehouseCustomerId){
        return JsonResult.success(warehouseSettledService.findAllContactByFactoryId(factoryId,warehouseCustomerId));
    }
}
